<?php

namespace common\core;

use Yii;
use yii\db\ActiveRecord;

class DBHelper extends ActiveRecord
{
    public $db;

    // 第一步：定义自己的事件
    const EVENT_BEFORE_MARK_QUERY = 'beforeMarkDelete';
    const EVENT_AFTER_MARK_QUERY = 'afterMarkDelete';

    public function init()
    {
        parent::init();
        $this->trigger(self::EVENT_INIT);
        // 完成绑定
        $this->on(self::EVENT_BEFORE_MARK_QUERY, [$this, 'onBeforeMarkQuery']);
        $this->on(self::EVENT_AFTER_MARK_QUERY, [$this, 'onAfterMarkQuery']);
        $this->db = $connection = Yii::$app->db2;
    }

    // 第二步：定义Event Handler
    public function onBeforeMarkQuery()
    {
        //print_r(Yii::$app->getControllerPath()) ;
        //echo Yii::$app->controller->route;
       // echo '<br/><br/><br/>';
        //var_dump(Yii::$app);
        //print_r(Yii::powered());
        //exit;
        //echo date("Y-m-d H:i:s",time());exit;
        //exit;
    }

    // 第二步：定义Event Handler
    public function onAfterMarkQuery()
    {
    }

    public function row($sql)
    {
        $this->trigger(self::EVENT_BEFORE_MARK_QUERY);


        $row = $this->db->createCommand($sql)
            ->queryOne();


        $this->trigger(self::EVENT_AFTER_MARK_QUERY);
        return $row;
    }

    public function rows($sql)
    {
        $row = $this->db->createCommand($sql)
            ->queryAll();
        return $row;
    }

    //__set()方法用来设置私有属性
    public function __set($name, $value)
    {
        $this->$name = $value;
    }

//__get()方法用来获取私有属性
    public function __get($name)
    {
        if (isset($this->$name))
            return $this->$name;
        return null;
    }

}
